// 获取天数或者指定天数 year年 month月
function timeGetMonth(year, month) {
    let day = new Date(year, month, 0);
    return JSON.stringify(day.getDate()).length < 2 ? `0${day.getDate()}`:day.getDate()
}
// 获取某一天是周几 // 2021-11-09
function getWeekDate(date) {
    let now = new Date(date);
    let day = now.getDay();
    let weeks = new Array("日","一","二","三","四","五","六");
    let week = weeks[day];
    return week;
}
// 获取 时间数组

function timeyear(yeartime,monthtime) {
    let date = new Date();
    let year = yeartime || date.getFullYear(); //获取当前年份
    let month = monthtime || (JSON.stringify(date.getMonth()).length < 2 ? `0${date.getMonth() + 1}`:date.getMonth()+1); //获取当前月份(0-11,0代表1月)
    let dayTime = timeGetMonth(year, month, 0); // 获取当前月份天数
    let timeList = {
        name: `${year}年${month}月`,
        key: `${year}-${month}`,
        list: []
    }
    for(let i = 1; i <= dayTime; i++) {
        let timeobj = {
            dayTime: i,
            weekTime: getWeekDate(`${year}-${month}-${JSON.stringify(i).length < 2 ? `0${i}`:i}`),
            keylist: `${year}-${month}-${JSON.stringify(i).length < 2 ? `0${i}`:i}`
        }
        timeobj.showClose = timeobj.weekTime == '六' ? true:timeobj.weekTime == '日' ? true:false
        timeList.list.push(timeobj)
    }
    return [timeList]
}

// 获取时间差的时间
function getDiffDay(date_1, date_2) {
	// 防止苹果时间计算出现NAN
	date_1 = date_1.replace(/-/g,'/')
	date_2 = date_2.replace(/-/g,'/')
	date_1 = date_1.replace(/T/g,' ')
	date_2 = date_2.replace(/T/g,' ')
	
    // 计算两个日期之间的差值
    let totalDays,diffDate
    let myDate_1 = Date.parse(date_1)
    let myDate_2 = Date.parse(date_2)
    // 将两个日期都转换为毫秒格式，然后做差
    diffDate = Math.abs(myDate_1 - myDate_2) // 取相差毫秒数的绝对值
    totalDays = Math.floor(diffDate / (1000 * 3600 * 24)) // 向下取整
    return totalDays    // 相差的天数
}

//计算日期之间的月数
function datemonth(date1,date2){
    // 拆分年月日
    date1 = date1.split('-');
    // 得到月数
    date1 = parseInt(date1[0]) * 12 + parseInt(date1[1]);
    // 拆分年月日
    date2 = date2.split('-');
    // 得到月数
    date2 = parseInt(date2[0]) * 12 + parseInt(date2[1]);
    var m = Math.abs(date1 - date2);
    return m;
}
// 获取当前时间
function getNowTime() {
	var date = new Date();
	//年 getFullYear()：四位数字返回年份
	var year = date.getFullYear(); //getFullYear()代替getYear()
	//月 getMonth()：0 ~ 11
	var month = date.getMonth() + 1;
	//日 getDate()：(1 ~ 31)
	var day = date.getDate();
	//时 getHours()：(0 ~ 23)
	var hour = date.getHours();
	//分 getMinutes()： (0 ~ 59)
	var minute = date.getMinutes();
	//秒 getSeconds()：(0 ~ 59)
	var second = date.getSeconds();
	return `${year}-${conversion(month)}-${conversion(day)}T${conversion(hour)}:${conversion(minute)}:${conversion(minute)}.122`
}
function conversion(code) {
	return JSON.stringify(code).length < 2 ? `0${code}`:code
}
export  {
    timeyear,
    getDiffDay,
	datemonth,
	getNowTime
}